<?php
// Kalenterin funktiot ja otsikot
include("functions.php");
include("calendarheaders.php");

$monthview = false;
if(strcmp($_GET["monthview"],"true") == 0)
    $monthview = true;

if(is_numeric($_GET["offset"]))
    $offset = $_GET["offset"];
else
    $offset = 0;

header("Content-type: image/png");

if(!$monthview) // Viikkonäkymä
{
    define("IMAGE_WIDTH",  COLUMN_WIDTH * 8);
    define("IMAGE_HEIGHT", 2 * HEADER_HEIGHT + 24 * HOUR_HEIGHT);
    
    $im = imagecreatetruecolor(IMAGE_WIDTH,IMAGE_HEIGHT);
    
    $background_color = imagecolorallocate($im,255,255,255);
    $line_color = imagecolorallocate($im,0,0,0);
    $fill_color["lectures"] = imagecolorallocatealpha($im,255,0,0,32);
    $fill_color["meetings"] = imagecolorallocatealpha($im,255,255,0,32);
    $fill_color["taught"] = imagecolorallocatealpha($im,0,255,0,32);
    $fill_color["supervised"] = imagecolorallocatealpha($im,0,255,255,32);
    $fill_color["exams"] = imagecolorallocatealpha($im,128,128,128,32);
    $fill_color["background"] = imagecolorallocatealpha($im,128,128,128,32);
    
    imagefill($im,0,0,$background_color);
    
    imagefilledrectangle($im,0*COLUMN_WIDTH+3,3,0*COLUMN_WIDTH+12,12,$fill_color["lectures"]);
    imagestring($im,TEXT_FONT,0*COLUMN_WIDTH+15,3,"Luento (Oppilas)",$line_color);
    
    imagefilledrectangle($im,1.5*COLUMN_WIDTH+3,3,1.5*COLUMN_WIDTH+12,12,$fill_color["meetings"]);
    imagestring($im,TEXT_FONT,1.5*COLUMN_WIDTH+15,3,"Harjoituskerta (Oppilas)",$line_color);
    
    imagefilledrectangle($im,3*COLUMN_WIDTH+3,3,3*COLUMN_WIDTH+12,12,$fill_color["taught"]);
    imagestring($im,TEXT_FONT,3*COLUMN_WIDTH+15,3,"Luento (Opettaja)",$line_color);
    
    imagefilledrectangle($im,4.5*COLUMN_WIDTH+3,3,4.5*COLUMN_WIDTH+12,12,$fill_color["supervised"]);
    imagestring($im,TEXT_FONT,4.5*COLUMN_WIDTH+15,3,"Harjoituskerta (Valvoja)",$line_color);
    
    imagefilledrectangle($im,6*COLUMN_WIDTH+3,3,6*COLUMN_WIDTH+12,12,$fill_color["exams"]);
    imagestring($im,TEXT_FONT,6*COLUMN_WIDTH+15,3,"Koe (Oppilas)",$line_color);
    
    imagefilledrectangle($im,0,HEADER_HEIGHT,IMAGE_WIDTH,2*HEADER_HEIGHT,$fill_color["background"]);
    
    // Tulosta sarakerajat
    for($i = 1; $i < 8; ++$i)
        imageline($im,COLUMN_WIDTH*$i,HEADER_HEIGHT,COLUMN_WIDTH*$i,IMAGE_HEIGHT,$line_color);

    // Tulosta tuntirajat
    for($i = 0; $i < 24; ++$i)
    {
        imagestring($im,HEADER_FONT-1,3,2*HEADER_HEIGHT+$i*HOUR_HEIGHT,str_pad($i,2,"0",STR_PAD_LEFT).":00",$line_color);
        imageline($im,0,2*HEADER_HEIGHT+$i*HOUR_HEIGHT,IMAGE_WIDTH,2*HEADER_HEIGHT+$i*HOUR_HEIGHT,$line_color);
    }
    
    $week_start = mktime(0,0,0);  $week_start += $offset * 7 * 24 * 60 * 60;
    while(date("N",$week_start) != 1)
        $week_start -= 24 * 60 * 60;
    
    imagestring($im,HEADER_FONT,3,HEADER_HEIGHT,date("W/Y",$week_start),$line_color);

    //Tulosta viikonpäivät
    for($i = 1; $i <= 7; ++$i)
    {
        $date = date(" d.m",$week_start + ($i - 1) * 24 * 60 * 60);
        switch($i)
        {
            case 1: $day = "Ma"; break;
            case 2: $day = "Ti"; break;
            case 3: $day = "Ke"; break;
            case 4: $day = "To"; break;
            case 5: $day = "Pe"; break;
            case 6: $day = "La"; break;
            case 7: $day = "Su"; break;
        }
        imagestring($im,HEADER_FONT,$i*COLUMN_WIDTH+3,HEADER_HEIGHT,$day.$date,$line_color);
    }
    
    // Aloita kalenterin täyttäminen
    $entries = $Calendar->get_entries($user,$offset);
    
    foreach($entries as &$entry)
    {
        $type = $entry->type;
        $start_time = $entry->start_time;
        $end_time = $entry->end_time;
        $location = $entry->location;
        $name = $entry->course;
        
        $x1 = date("N",$start_time) * COLUMN_WIDTH;
        $x2 = $x1 + COLUMN_WIDTH;
        $y1 = 2 * HEADER_HEIGHT + date("H",$start_time) * HOUR_HEIGHT + date("i",$start_time) * 60 / HOUR_HEIGHT;
        $y2 = 2 * HEADER_HEIGHT + date("H",$end_time) * HOUR_HEIGHT + date("i",$end_time) * 60 / HOUR_HEIGHT;
        imagefilledrectangle($im,$x1,$y1,$x2,$y2,$fill_color[$type]);
        imagerectangle($im,$x1,$y1,$x2,$y2,$line_color);
        
        imagestring($im,TEXT_FONT,$x1+3,$y1+3,substr($name,0,18),$line_color);
        imagestring($im,TEXT_FONT,$x1+3,$y1+3+HEADER_HEIGHT,substr($location,0,18),$line_color);
    }
}
else // Kuukausinäkymä
{
    define("IMAGE_WIDTH", 8 * CELL_WIDTH);
    define("IMAGE_HEIGHT", 7 * CELL_HEIGHT);
    
    $im = imagecreatetruecolor(IMAGE_WIDTH,IMAGE_HEIGHT);
    
    $background_color = imagecolorallocate($im,255,255,255);
    $fill_color = imagecolorallocatealpha($im,128,128,128,32);
    $line_color = imagecolorallocate($im,0,0,0);
    
    imagefill($im,0,0,$background_color);
    
    imagefilledrectangle($im,0,0,IMAGE_WIDTH,CELL_HEIGHT,$fill_color);
    imagefilledrectangle($im,0,CELL_HEIGHT,CELL_WIDTH,IMAGE_HEIGHT,$fill_color);
    
    // Piirrä sarakerajat
    for($i = 1; $i <= 7; ++$i)
        imageline($im,$i*CELL_WIDTH,0,$i*CELL_WIDTH,IMAGE_HEIGHT,$line_color);
    // Piirrä rivirajat
    for($i = 1; $i <= 6; ++$i)
        imageline($im,0,$i*CELL_HEIGHT,IMAGE_WIDTH,$i*CELL_HEIGHT,$line_color);
    
    $date = mktime(0,0,0,date("n")+$offset,1);
    imagestring($im,HEADER_FONT,3,0,date("m/y",$date),$line_color);
    
    imagestring($im,HEADER_FONT,1*CELL_WIDTH+3,0,"Ma",$line_color);
    imagestring($im,HEADER_FONT,2*CELL_WIDTH+3,0,"Ti",$line_color);
    imagestring($im,HEADER_FONT,3*CELL_WIDTH+3,0,"Ke",$line_color);
    imagestring($im,HEADER_FONT,4*CELL_WIDTH+3,0,"To",$line_color);
    imagestring($im,HEADER_FONT,5*CELL_WIDTH+3,0,"Pe",$line_color);
    imagestring($im,HEADER_FONT,6*CELL_WIDTH+3,0,"La",$line_color);
    imagestring($im,HEADER_FONT,7*CELL_WIDTH+3,0,"Su",$line_color);
    
    $month = date("n",$date);
    while(date("N",$date) != 1)
        $date -= 24 * 60 * 60;
        
    for($y = 1; $y < 7; ++$y)
    {
        imagestring($im,HEADER_FONT,3,$y*CELL_HEIGHT,date("W",$date),$line_color);
        for($x = 1; $x < 8; ++$x)
        {
            if(date("n",$date) == $month)
                $color = $line_color;
            else
                $color = $fill_color;
            
            imagestring($im,HEADER_FONT,$x*CELL_WIDTH+3,$y*CELL_HEIGHT,date("j",$date),$color);
            $date += 24 * 60 * 60;
        }
    }
}

imagepng($im);
destroyimage($im);

?>
